package com.niiwoo.civet.user.service.duboo;

import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSON;
import com.niiwoo.civet.user.dao.entity.DataStatus;
import com.niiwoo.civet.user.dao.entity.LoginAccountMapping;
import com.niiwoo.civet.user.dto.request.QueryUserBasicInfoRequestDTO;
import com.niiwoo.civet.user.dto.request.UpdateUserBasicInfIMRequestDTO;
import com.niiwoo.civet.user.dto.response.ImGetUserBasicInfoResponseDTO;
import com.niiwoo.civet.user.enums.QueryBasicInfoEnum;
import com.niiwoo.civet.user.enums.UserDataTypeEnum;
import com.niiwoo.civet.user.service.ImDubboService;
import com.niiwoo.civet.user.service.local.UserBasicInfoService;
import com.niiwoo.civet.user.service.local.UserLoginAccountService;
import com.niiwoo.civet.user.service.local.UserStatusService;
import com.niiwoo.tripod.base.component.PrivacyMasks;
import com.niiwoo.tripod.im.component.IMService;
import com.niiwoo.tripod.im.request.UpdateUserInfoRequest;
import com.niiwoo.tripod.provider.exception.BizException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.concurrent.CompletableFuture;

/**
 * Created by huangmin on 2017/12/19.
 */
@Slf4j
@Service(version = "1.0.0")
public class ImDubboServiceImpl implements ImDubboService {

    @Autowired
    private IMService imService;
    @Autowired
    private PrivacyMasks privacyMasks;
    @Autowired
    private UserStatusService userStatusService;
    @Autowired
    private UserBasicInfoService userBasicInfoService;
    @Autowired
    private UserLoginAccountService userLoginAccountLocalService;

    @Override
    public ImGetUserBasicInfoResponseDTO imGetUserBasicInfo(QueryUserBasicInfoRequestDTO requestDTO) {
        if(QueryBasicInfoEnum.MOBILE_NO.equals(requestDTO.getType())){
            LoginAccountMapping loginAccountMapping = userLoginAccountLocalService.getAccountMapping(privacyMasks.encryptPrivacy(requestDTO.getMobileNo()));
            if(loginAccountMapping == null)
                return null;
            requestDTO.setUserId(loginAccountMapping.getUserId());
        }
        ImGetUserBasicInfoResponseDTO responseDTO = userBasicInfoService.selectOccupationInfoByUserId(requestDTO.getUserId());
        if(responseDTO != null){
            responseDTO.setMobile(privacyMasks.decryptPrivacy(responseDTO.getMobile()));
            DataStatus dataStatus = userStatusService.getByUserIdAndType(requestDTO.getUserId(), UserDataTypeEnum.TWO_ELEMENT);
            responseDTO.setIdentity(dataStatus == null ? Byte.valueOf("0") : Byte.valueOf("1"));
        }
        return responseDTO;
    }

    @Override
    public void updateUserBasicInfIM(UpdateUserBasicInfIMRequestDTO requestDTO) {
        if(requestDTO.getGuid() == null || requestDTO.getGuid().equals("")){
            log.error("ImDubboServiceImpl.updateUserBasicInfIM userId={}", requestDTO.getGuid());
            throw new BizException("USR10019");
        }
        UpdateUserInfoRequest requestIM = new UpdateUserInfoRequest();
        requestIM.setGuid(requestDTO.getGuid());
        requestIM.setNickname(requestDTO.getNickname());
        requestIM.setAvatar(requestDTO.getAvatar());
        requestIM.setMobile(requestDTO.getMobile());
        requestIM.setIntroduction(requestDTO.getIntroduction());
        requestIM.setGender(requestDTO.getGender());
        requestIM.setPassword(requestDTO.getPassword());
        requestIM.setAddIdentity(requestDTO.getAddIdentity());
        requestIM.setDelIdentity(requestDTO.getDelidentity());
        requestIM.setCompany(requestDTO.getCompany());
        requestIM.setOccupation(requestDTO.getOccupation());
        requestIM.setIndustry(requestDTO.getIndustry());
        requestIM.setArea(requestDTO.getArea());

        log.info("ImDubboServiceImpl.updateUserBasicInfIM.result userId={}, response={}", requestDTO.getGuid(),JSON.toJSON(requestIM));
        CompletableFuture.runAsync(() -> imService.updateUserInfo(requestIM));
    }

}
